﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.COM;
using Excel;
using System.IO;
using System.Reflection;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M013_STORAGE_Detail : FormBase
    {
        public M013_STORAGE_Detail(string strGoodID)
        {
            this.strGoodID = strGoodID.Trim();
            InitializeComponent();
        }

        #region 变量定义

        BLL.BLL_M014BILLSCAN bll_M014BILLSCAN = new XiaoGang.Soft.XGWMS.BLL.BLL_M014BILLSCAN();
        BLL.BLL_M007_GOODS bll_M007 = new XiaoGang.Soft.XGWMS.BLL.BLL_M007_GOODS();
        string strGoodID = "";
        private const string dataName = "商品流水";
       
        #endregion

        #region 窗口初始化

        protected void SetTableIndex()
        {
            int intIndex = 0;
            btnClose.TabIndex = intIndex; intIndex++;
        }

        /// <summary>
        /// <p><b>方法名称</b>InitialGridView</p>
        /// <p><b>概要説明	:</b>Grid初始化</p>
        /// <p><b>作成者	:</b>万岗</p>
        /// <p><b>作成日	:</b>2008/09/10</p>
        /// </summary>
        protected void InitialGridView()
        {
            grdmentBillDetailInfo.Height = 379;
            grdmentBillDetailInfo.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            grdmentBillDetailInfo.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            grdmentBillDetailInfo.ReadOnly = true;
            grdmentBillDetailInfo.ColumnHeadersHeight = 20;
            grdmentBillDetailInfo.ColumnHeadersVisible = true;
            grdmentBillDetailInfo.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;

            grdmentBillDetailInfo.Columns.Add("NO.", "NO.");
            grdmentBillDetailInfo.Columns.Add("BUSINESS_TIME", "时间");
            grdmentBillDetailInfo.Columns.Add("IN_OUT_Name", "业务类型");
            grdmentBillDetailInfo.Columns.Add("UNITS", "数量");
            grdmentBillDetailInfo.Columns.Add("PRICE", "价格");
            grdmentBillDetailInfo.Columns.Add("MEMO", "备注");

            grdmentBillDetailInfo.Columns["NO."].Width = 30;
            grdmentBillDetailInfo.Columns["NO."].Frozen = true;
            grdmentBillDetailInfo.Columns["NO."].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentBillDetailInfo.Columns["BUSINESS_TIME"].Width = 120;
            grdmentBillDetailInfo.Columns["IN_OUT_Name"].Width = 120;
            grdmentBillDetailInfo.Columns["IN_OUT_Name"].Frozen = true;
            grdmentBillDetailInfo.Columns["IN_OUT_Name"].Resizable = DataGridViewTriState.False;
            grdmentBillDetailInfo.Columns["UNITS"].Width = 80;
            grdmentBillDetailInfo.Columns["PRICE"].Width = 80;
            //grdmentBillDetailInfo.Columns["MEMO"].Width = 350;
            grdmentBillDetailInfo.Columns["BUSINESS_TIME"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentBillDetailInfo.Columns["BUSINESS_TIME"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentBillDetailInfo.Columns["IN_OUT_Name"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentBillDetailInfo.Columns["IN_OUT_Name"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentBillDetailInfo.Columns["PRICE"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdmentBillDetailInfo.Columns["PRICE"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdmentBillDetailInfo.Columns["UNITS"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdmentBillDetailInfo.Columns["UNITS"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdmentBillDetailInfo.AllowUserToAddRows = false;
            grdmentBillDetailInfo.AllowUserToResizeColumns = true;
            grdmentBillDetailInfo.AllowUserToResizeRows = false;
            grdmentBillDetailInfo.RowHeadersVisible = false;
            grdmentBillDetailInfo.RowTemplate.Resizable = DataGridViewTriState.False;
            grdmentBillDetailInfo.RowTemplate.Height = 20;
            grdmentBillDetailInfo.RowTemplate.ReadOnly = true;
        }

        /// <summary>
        /// <p><b>方法名称</b>InitProcess</p>
        /// <p><b>概要説明	:</b>画面的初期化</p>
        /// <p><b>作成者	:</b>万岗</p>
        /// <p><b>作成日	:</b>2008/09/10</p>
        /// </summary>
        protected void InitProcess()
        {
            lbGOODS_ID.Text = "";
            lbGOODS_NM1.Text = "";
            lbAllUnits.Text = "";
            lbGOODS_GROUP.Text = "";
            lbPROVIDER.Text = "";
            lbMEMO.Text = "";
            InitialGridView();
            SetTableIndex();
            ShowGrid(strGoodID);
            grpBillDetailInfo.Text = "共有" +  grdmentBillDetailInfo.Rows.Count + "条商品流水信息";
        }

        #endregion

        #region 方法

        /// <summary>
        /// <p><b>方法名称</b>ShowGrid</p>	
        /// <p><b>概要説明  :</b>Grid data display</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <param type="string" name="strGoodID">货单编号</param>
        protected void ShowGrid(string strGoodID)
        {
            ReturnValue rvGoods;
            MODEL.MOD_M007_GOODS modelM007=new XiaoGang.Soft.XGWMS.MODEL.MOD_M007_GOODS();
            modelM007.GOODS_ID=strGoodID;
            rvGoods = bll_M007.GetSingleData(modelM007);
            if (rvGoods.IsSuccess == false)
            {
                Method.MessageShow(rvGoods.ErrorID, dataName);
                return;
            }

            if (rvGoods.ResultDataSet.Tables[0].Rows.Count > 0)
            {
                lbGOODS_ID.Text = strGoodID;
                lbGOODS_NM1.Text = rvGoods.ResultDataSet.Tables[0].Rows[0]["GOODS_NM1"].ToString();
                lbGOODS_GROUP.Text = rvGoods.ResultDataSet.Tables[0].Rows[0]["GOODS_GROUP_NM"].ToString();
                lbPROVIDER.Text = rvGoods.ResultDataSet.Tables[0].Rows[0]["PROVIDER"].ToString();
                lbMEMO.Text = rvGoods.ResultDataSet.Tables[0].Rows[0]["MEMO"].ToString();
            }

            //取得商品[进][出][总]合计
            rvGoods = bll_M014BILLSCAN.GetAllUnits(strGoodID);
            if (rvGoods.IsSuccess == false)
            {
                Method.MessageShow(rvGoods.ErrorID, dataName);
                return;
            }
            if (rvGoods.ResultDataSet.Tables[0].Rows.Count > 0)
            {
                lbAllUnits.Text = "入库：[" + rvGoods.ResultDataSet.Tables[0].Rows[0]["ALLUNITS_IN"].ToString()
                    + "] 销售：[" + rvGoods.ResultDataSet.Tables[0].Rows[0]["ALLUNITS_OUT"].ToString()
                    + "] 库存：[" + rvGoods.ResultDataSet.Tables[0].Rows[0]["ALLUNITS"].ToString()+"]";
            }

            rvGoods = bll_M014BILLSCAN.SearchStorageDetail(strGoodID);

            if (rvGoods.IsSuccess == false)
            {
                Method.MessageShow(rvGoods.ErrorID, dataName);
                return;
            }            
            grdmentBillDetailInfo.Rows.Clear();
            foreach (DataRow row in rvGoods.ResultDataSet.Tables[0].Rows)
            {
                grdmentBillDetailInfo.Rows.Add(
                    (grdmentBillDetailInfo.Rows.Count + 1).ToString(),
                    DateFormat(row["BUSINESS_TIME"].ToString().Replace("/","")),
                    row["IN_OUT_Name"].ToString().Trim(),
                    row["UNITS"].ToString().Trim(),
                    row["PRICE"].ToString().Trim()== "-1.0000" ? "" : string.Format("{0:f2}",decimal.Parse(row["PRICE"].ToString().Trim())),
                    row["MEMO"].ToString().Trim());
            }
            if (grdmentBillDetailInfo.Rows.Count > 17)
            {
                grdmentBillDetailInfo.Columns["MEMO"].Width = 305;
            }
            else
            {
                grdmentBillDetailInfo.Columns["MEMO"].Width = 322;
            }
        }

        /// <summary>	
        /// <p><b>方法名称</b>DateFormat</p>	
        /// <p><b>概要説明  :</b>date data format</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <param name="strValue">没有格式化的字符串</param> 
        /// <returns type="string">格式化后的字符串</returns>
        protected string DateFormat(string strValue)
        {
            if (strValue.Length < 8)
            {
                return strValue;
            }
            return strValue.Substring(0, 4) + "/" +
                strValue.Substring(4, 2) + "/" +
                strValue.Substring(6, 2);

        }

        /// <summary>	
        /// <p><b>方法名称</b>NumPriceFormat</p>	
        /// <p><b>概要説明  :</b>num and price data format</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// <p><b>修改历史:</b></p>	
        /// </summary>
        /// <param name="strValue">没有格式化的字符串</param> 
        /// <returns type="string">格式化后的字符串</returns>
        protected string NumPriceFormat(string strValue)
        {
            string strTemp = strValue;
            if (strValue.Length > 6)
            {
                strTemp = strValue.Substring(0, strValue.Length - 6) + ","
                    + strValue.Substring((strValue.Substring(0, strValue.Length - 6)).Length, 3)
                    + "," + strValue.Substring(strValue.Length - 3, 3);

            }
            else if (strValue.Length > 3)
            {
                strTemp = strValue.Substring(0, strValue.Length - 3) + ","
                + strValue.Substring((strValue.Substring(0, strValue.Length - 3)).Length, 3);
            }
            return strTemp;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="myExcel"></param>
        /// <param name="dsData"></param>
        private void WriteExlWithDataTable(Excel.Application myExcel, DataSet dsData)
        {
            Excel.Workbook myBook = myExcel.Workbooks[1];
            Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1];
            Excel.Range r;
            Excel.Range r1;
            for (int i = 0; i < dsData.Tables[0].Rows.Count; i++)
            {
                r = (Excel.Range)mySheet.Rows.get_Item(i + 6, Missing.Value);
                r.Copy(Missing.Value);
                r1 = (Excel.Range)mySheet.Rows.get_Item(i + 7, Missing.Value);
                r1.PasteSpecial(XlPasteType.xlPasteFormats, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
                myExcel.Cells[i + 6, 1] = "'" + (i + 1).ToString();
                myExcel.Cells[i + 6, 2] = "'" + dsData.Tables[0].Rows[i]["GOODS_ID"].ToString();
                //myExcel.Cells[i + 6, 3] = "'" + dsData.Tables[0].Rows[i]["GOODS_NO"].ToString();
                myExcel.Cells[i + 6, 3] = "'" + dsData.Tables[0].Rows[i]["GOODS_NM1"].ToString();
                myExcel.Cells[i + 6, 4] = "'" + dsData.Tables[0].Rows[i]["GOODS_NM2"].ToString();
                myExcel.Cells[i + 6, 5] = "'" + dsData.Tables[0].Rows[i]["GOODS_GROUP"].ToString();
                myExcel.Cells[i + 6, 6] = "'" + dsData.Tables[0].Rows[i]["PROVIDER"].ToString();
                myExcel.Cells[i + 6, 7] = "'" + dsData.Tables[0].Rows[i]["PRODUCE_DATE"].ToString();
                myExcel.Cells[i + 6, 8] = "'" + dsData.Tables[0].Rows[i]["OVER_PRODUCE_DATE"].ToString();
                //myExcel.Cells[i + 6, 10] = "'" + dsData.Tables[0].Rows[i]["OVER_DATE"].ToString();
                //myExcel.Cells[i + 6, 11] = "'" + dsData.Tables[0].Rows[i]["MAX_WARN"].ToString();
                //myExcel.Cells[i + 6, 12] = "'" + dsData.Tables[0].Rows[i]["MIN_WARN"].ToString();
                myExcel.Cells[i + 6, 9] = "'" + dsData.Tables[0].Rows[i]["BUYING_PRICE"].ToString();
                myExcel.Cells[i + 6, 10] = "'" + dsData.Tables[0].Rows[i]["SELLING_PRICE"].ToString();
                //myExcel.Cells[i + 6, 15] = "'" + dsData.Tables[0].Rows[i]["MEMO"].ToString();
            }
        }

        /// <summary>
        /// SaveFile
        /// </summary>
        /// <param name="strMobanName"></param>
        /// <param name="filename"></param>
        private void SaveFile(string strMobanName, out string filename)
        {
            SaveFileDialog mySave = new SaveFileDialog();
            mySave.Filter = "Excelﾎﾄｼ(*.XLS)|*.xls|ﾋﾐﾎﾄｼ(*.*)|*.*";
            if (mySave.ShowDialog() != DialogResult.OK)
            {
                filename = null;
                return;
            }
            else
            {
                filename = mySave.FileName;
                string filenameold = mySave.FileName;
                FileInfo mode = new FileInfo(System.Windows.Forms.Application.StartupPath.Trim() + "\\" + strMobanName);
                try
                {
                    mode.CopyTo(filename, true);
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message);
                    return;
                }
            }
        }

        #endregion

        #region 事件

        private void M013_STORAGE_Detail_Load(object sender, EventArgs e)
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitProcess();
        }

        /// <summary>
        /// btnClose_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            this.Close();
        }

        /// <summary>
        /// M013_STORAGE_Detail_KeyUp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M013_STORAGE_Detail_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    btnClose_Click(sender, e);
                    break;
            }
        }

        #endregion
    }
}